﻿using System;
using System.Text.RegularExpressions;
using BO.Infrastructure.StaticData;
using BO.Infrastructure.Utils;
using BO.News;
using ChannelVN.Cached.UrlRewrite;
using GUI.Extensions;

namespace GUI.Pages.News
{
    public partial class ListNewsByThread : System.Web.UI.Page
    {
        private const int PageSize = 15;
        protected void Page_Load(object sender, EventArgs e)
        {
            var objThread = RequestExtensions.Tag;
            if (objThread != null)
            {
                var containsPageIndex = RequestExtensions.ContainsPageIndex;
                var pageIndex = RequestExtensions.PageIndex;
                var canonicalLink = containsPageIndex ? PresentationUtils.UrlBuilder.BuildPagingUrl(objThread.Url, pageIndex) : objThread.Url;

                if (!Request.RawUrl.StartsWith(canonicalLink))
                {
                    _301Redirection.RedirectTo(canonicalLink);
                }
                PresentationUtils.SetCanonicalLink(Page, canonicalLink);

                var dt = NewsBO.GetInstance().GetNewsByTag(tagId: objThread.Id, pageIndex: pageIndex, pageSize: PageSize);
                ctrListNews.Visible = ctrListNews.DataBind(dt);

                ltrTagName.Text = objThread.Name;


                if (ctrListNews.Visible)
                {
                    ctrPaging.SumPage = NewsBO.GetInstance().GetTotalPagesOfListNewsByTag(tagId: objThread.Id, pageSize: PageSize);
                    ctrPaging.LinkSite = RequestExtensions.Url;
                    ctrPaging.Padding(pageIndex);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        var r = dt.Rows[0];
                        ctrGoogleSnippet.Url = PresentationUtils.UrlBuilder.BuildAbsoluteUrl(objThread.Url);
                        ctrGoogleSnippet.Title = objThread.Name;
                        ctrGoogleSnippet.Description = r[SqlConstants.NewsPublish.Sapo].ToString();
                        ctrGoogleSnippet.Avatar = PresentationUtils.Photo.GetSourceImage(r[SqlConstants.NewsPublish.Avatar].ToString());
                        PresentationUtils.AddMeta(Page, DefinedConstants.MetaAttributes.Description.Id, DefinedConstants.MetaAttributes.Description.Name, r[SqlConstants.NewsPublish.Sapo].ToString());
                    }
                }

                #region Title/MetaData
                var extraText = containsPageIndex ? string.Format(" | page {0}", pageIndex) : string.Empty;
                Page.Header.Title = string.Format("{0} | soha.vn{1}", objThread.Name, extraText);

                PresentationUtils.AddMeta(Page, DefinedConstants.MetaAttributes.Keywords.Id, DefinedConstants.MetaAttributes.Keywords.Name, objThread.Name);
                #endregion
                return;
            }
            var suggest = string.Empty;
            if (!string.IsNullOrEmpty(Request.RawUrl))
            {
                try
                {
                    suggest = Regex.Split(Request.RawUrl, "/([^/]+)-([et0-9]+).htm")[1];
                }
                catch (Exception)
                {
                    suggest = string.Empty;
                }
            }
            _301Redirection.RedirectToFileNotFoundPage(suggest);
        }
    }
}
